<template>
  <div class="profile-box">
    <div class="user-header">
      <div class="user-info" v-if="!loginFlag">
        <div class="user-head-portrait">
          <img src="@/assets/imgs/下载.png" alt="" />
        </div>
        <div class="login-button">
          <span @click="goLogin">登录</span>
          <span> / </span>
          <span @click="goRegister">注册</span>
        </div>
      </div>
      <div class="user-data" v-else>
        <div class="user-top">
          <div class="image">
            <img :src="userInfo.photo" alt="" />
          </div>
          <div class="phone">{{ userInfo.name }}</div>
          <div class="edit-box">
            <div class="edit-btn">编辑资料</div>
          </div>
        </div>
        <div class="user-item-box">
          <div class="user-item">
            <span class="count">{{ userInfo.art_count }}</span>
            <span>头条</span>
          </div>
          <div class="user-item">
            <span class="count">{{ userInfo.fans_count }}</span>
            <span>粉丝</span>
          </div>
          <div class="user-item">
            <span class="count">{{ userInfo.follow_count }}</span>
            <span>关注</span>
          </div>
          <div class="user-item">
            <span class="count">{{ userInfo.like_count }}</span>
            <span>获赞</span>
          </div>
        </div>
      </div>
    </div>
    <div class="my-grid">
      <div class="Collection">
        <van-icon
          class-prefix="toutiao-shoucang toutiao shoucang"
          name="extra"
        />
        <span>收藏</span>
      </div>
      <div class="history">
        <van-icon class-prefix="toutiao-lishi toutiao shoucang" name="extra" />
        <span>历史</span>
      </div>
    </div>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <div class="signout" v-if="loginFlag" @click="signOut">退出</div>
  </div>
</template>

<script>
import { user } from '@/api/user'
import { removeUserToken, removeUserData } from '@/utils/storage'
import { Dialog } from 'vant'

export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      loginFlag: false,
      userInfo: {},
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getuserInfo() // 获取用户信息
  },
  methods: {
    // 获取用户信息
    async getuserInfo() {
      try {
        const {
          data: { data },
        } = await user()
        console.log(data)
        this.userInfo = data
        this.$store.commit('user/setUserInfoData', data)
        this.loginFlag = true
      } catch (error) {
        removeUserToken()
        removeUserData()
        this.loginFlag = false
      }
    },
    // 去登录
    goLogin() {
      this.$router.push('login')
    },
    // 去注册
    goRegister() {
      this.$router.push('login')
    },
    // 退出登录
    async signOut() {
      Dialog.confirm({
        title: '黑马头条',
        message: '是否确认退出该账号',
      })
        .then(() => {
          this.$store.dispatch('user/setUserInfo', null)
          removeUserToken()
          removeUserData()
          this.loginFlag = false
        })
        .catch(() => {
          // on cancel
        })
    },
  },
}
</script>

<style scoped lang="less">
.profile-box {
  background-color: #f5f7f9;
  min-height: 100vh;
}
.user-header {
  position: relative;
  overflow: hidden;
  width: 100vw;
  height: 200px;
  background: url('http://liufusong.top/hmtt/img/banner.d43e3812.png') no-repeat
    center center;
  background-size: 100% 100%;
  .user-info {
    position: absolute;
    width: 100%;
    top: 50%;
    transform: translate(0, -50%);
    .user-head-portrait {
      width: 66px;
      height: 66px;
      margin: 0 auto;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .login-button {
      margin-top: 5px;
      text-align: center;
      color: #fff;
      font-size: 14px;
    }
  }
}
.my-grid {
  display: flex;
  height: 81px;
  width: 100vw;
  padding-bottom: 3px;
  background-color: #f5f7f9;
  .Collection,
  .history {
    height: 78px;
    background-color: #fff;
    box-sizing: border-box;
    flex-direction: column;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    span {
      font-size: 12px;
    }
  }
  .Collection {
    border-right: 1px solid #f5f3f3;
  }
  .shoucang {
    font-size: 23px;
    color: #ff9d1d;
  }
}
.signout {
  margin-top: 5px;
  height: 44px;
  background-color: #fff;
  line-height: 44px;
  text-align: center;
  font-size: 14px;
  color: red;
}
.user-top {
  height: 68px;
  margin-top: 58px;
  padding: 0 16px;
  display: flex;
  .image {
    width: 68px;
    height: 68px;
    background-color: #fff;
    border-radius: 50%;
    img {
      margin: 1px;
      border-radius: 50%;
      width: 66px;
      height: 66px;
    }
  }
  .phone {
    margin-left: 10px;
    line-height: 68px;
    font-size: 14px;
    color: #fff;
  }
  .edit-box {
    flex: 1;
    line-height: 68px;
    text-align: right;
  }
  .edit-btn {
    display: inline-block;
    background: #fff;
    font-size: 12px;
    color: #000;
    width: 60px;
    height: 15px;
    text-align: center;
    line-height: 15px;
    border-radius: 7px;
  }
}
.user-item-box {
  height: 78px;
  display: flex;
  .user-item {
    width: 25%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 12px;
    color: #fff;
    .count {
      margin-bottom: 5px;
    }
  }
}
</style>
